﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataLib.DAL
{
    using System;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;
    using DataLib.DBUtility;


    /// <summary>
    /// 生产相关操作类
    /// </summary>
    public partial class Product
    {


        /// <summary>
        /// 取出库生产单号
        /// </summary>
        /// <returns>需要返回的序列号</returns>
        public string GetOutNo()
        {
            string str = "";
            DataTable dt = new DataTable();
            int maxserial = 0;
            string fieldname = "";
            int numlen = 7;
            maxserial = new DataLib.BLL.tab_Num().GetNoByType("WoutNo");


            if (maxserial == 0)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    string strtemp = dt.Rows[0][fieldname].ToString();
                    strtemp = strtemp.Remove(0, 1);
                    maxserial = int.Parse(strtemp) + 1;
                }
                else
                {
                    maxserial = 1;
                }
            }
            str = "W" + maxserial.ToString().PadLeft(numlen, '0');
            return str;
        }


        /// <summary>
        /// 根据barcodeID取得整个value数组
        /// </summary>
        /// <param name="arrbarid"></param>
        /// <returns></returns>
        public string GetBarcodeValueArr(string arrbarid)
        {
            string str = "";
            if (arrbarid != "")
            {
                string strtemp = arrbarid.Remove(arrbarid.Length - 1);
                DataTable dt = new DataTable();
                dt = new DataLib.BLL.BarCode().GetBarCodeValue(strtemp);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        str += dt.Rows[i]["barcodevalue"].ToString();
                        str += ",";
                    }
                    str = str.Remove(str.Length - 1);
                }
            }
            return str;
        }


        /// <summary>
        /// 出库提交事件
        /// </summary>
        /// <param name="model">OutStorage的实例对象</param>
        /// <param name="strarr">需要出库的BarCode列表</param>
        /// <returns></returns>
        public int OutStorageSubmit(Model.OutStorage model, string strarr)
        {
            int rowcount;
            int result = 0;
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@arrBarCode",SqlDbType.VarChar,6000),
                new SqlParameter("@outid",SqlDbType.Int),
                new SqlParameter("@outno",SqlDbType.VarChar,12),
                new SqlParameter("@oper",SqlDbType.VarChar,20),
                new SqlParameter("@belong",SqlDbType.VarChar,20),
                new SqlParameter("@graphicCity",SqlDbType.VarChar,20),
                new SqlParameter("@ConsumMan",SqlDbType.VarChar,20),
                new SqlParameter("@outStorageType",SqlDbType.VarChar,3),
                new SqlParameter("@docusm",SqlDbType.VarChar,200),
                new SqlParameter("@saleno",SqlDbType.VarChar,3),
                new SqlParameter("@clearmonth",SqlDbType.VarChar,6),
                new SqlParameter("@memo",SqlDbType.VarChar,300)

             };
            sqlparameter[0].Value = strarr;
            sqlparameter[1].Value = model.id;
            sqlparameter[2].Value = model.outStorageNo;
            sqlparameter[3].Value = model.Operator;
            sqlparameter[4].Value = model.belogarea;
            sqlparameter[5].Value = model.graphicCity;
            sqlparameter[6].Value = model.ConsumMan;
            sqlparameter[7].Value = model.outStorageType;
            sqlparameter[8].Value = model.DoCustomer;
            sqlparameter[9].Value = model.OutsNo;
            sqlparameter[10].Value = model.clearmonth;
            sqlparameter[11].Value = model.memo;
            result = DbHelperSQL.RunProcedure("EWMS_ProductOSSubmit", sqlparameter, out rowcount);
            return result;

        }


     
               /// <summary>
        /// 验证将要出库的BarCode是否合法
        /// </summary>
        /// <param name="barcodevalue"></param>
        /// <param name="outType"></param>
        /// <param name="returnvalue"></param>
        /// <returns></returns>
        public Model.BarCode OutBarCodeConfirm(string barcodevalue,string vload, ref string returnvalue)
        {
            Model.BarCode barModel = new Model.BarCode();
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@barcodevalue",SqlDbType.VarChar,35),
                new SqlParameter("@returnvalue",SqlDbType.VarChar,80),
                new SqlParameter("@vload",SqlDbType.VarChar,15)
             };
            sqlparameter[1].Direction = ParameterDirection.Output;

            sqlparameter[0].Value = barcodevalue;
            sqlparameter[1].Value = returnvalue;
            sqlparameter[2].Value = vload;
             SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_ProductOSConfirm", sqlparameter);
            dt.Load(dr);
            dr.Close();
            returnvalue = sqlparameter[1].Value.ToString();
            if (dt != null && dt.Rows.Count > 0)
            {
                barModel = new BarCode().DataRowToModel(dt.Rows[0]);
            }
            else
            {
                return null;
            }

            return barModel;
        
        }
    }
}
